const config = require('../../config.js')
import util from './util.js'
export default {
	async postLofin (data) {
		try {
			var result = await util.http.post('/login/login', data)
			return Promise.resolve(result)
		} catch (e) {
			return Promise.resolve(e)
		}
	},
	async getCuUser (id) {
		let data = {
			id: id
		}
		try {
			var result = await util.http.get('/csuser/getcsuser', data)
			console.log(result)
			return Promise.resolve(result)
		} catch (e) {
			return Promise.resolve(e)
		}
	},
	async getAlluser (id) {
		try {
			var result = await util.http.get('/csuser/getalluser', {butt_id: id})
			return Promise.resolve(result)
		} catch (e) {
			return Promise.resolve(e)
		}
	},
	async getMsguser (id) {
		try {
			// 服务器获取最新的消息用户
			var serverUser = await util.http.get('/user/autopushmsg', {cs_id: id})
			var serverUserList = []
			// 获取本地存储的消息用户
			try {
				var locationList = uni.getStorageSync('msguser').data || []
			} catch (e) {
				var locationList = []
			}
			if (serverUser.status === 1) serverUserList = serverUser.result
			serverUserList.forEach((item) => {
				var newmsg = item.chatlog[item.chatlog.length - 1] || {}
				item.last_butt_time = newmsg.send_time || 0
				item.butt_id = id
				if (newmsg.type && newmsg.type === 'text') {
					item.last_butt_msg = newmsg.content
				} else if (newmsg.type && newmsg.type === 'image') {
					item.last_butt_msg = '[图片]'
				} else if (newmsg.type && newmsg.type === 'audio') {
					item.last_butt_msg = '[语音]'
				} else if (newmsg.type && newmsg.type === 'video') {
					item.last_butt_msg = '[视频]'
				} else {
					item.last_butt_msg = '[新消息]'
				}
				delete item.chatlog
				var inLocationList = false
				locationList.forEach((item1) => {
					if (item1.user_id === item.user_id) {
						item1 = item
						inLocationList = true
					}
				})
				if (!inLocationList) {
					locationList.push(item)
				}
			})
			uni.setStorageSync('msguser', {
				data: locationList
			})
			return Promise.resolve(serverUserList)
		} catch (e) {
			return Promise.resolve(e)
		}
	},
	calcUsers (list) {
		var statis = {}
		list.forEach(function (item) {
      if (item.subscribe) {
        statis.subscribe = statis.subscribe || 0
        statis.subscribe += 1
				if (item.groupid) {
           statis.groupid = statis.groupid || 0
           statis.groupid += 1
        } else {
          statis.ungroupid = statis.ungroupid || 0
          statis.ungroupid += 1
        }
      } else {
        statis.unsubscribe = statis.unsubscribe || 0
        statis.unsubscribe += 1
      }
    })
		return statis
	},
	async loadingRes ($ws) {
		try {
			var data = {}
			// 获取客服信息
			var csuser = uni.getStorageSync('csuser')
			csuser.info = await this.getCuUser(csuser.cs_id)
			csuser.info = csuser.info.cs_info
			uni.setStorageSync('csuser', csuser)
			console.log(csuser)
			data.csuser = csuser.info
			// 获取消息列表
			var msguser = await this.getMsguser(csuser.cs_id)
			msguser.sort(function (v1, v2) {
        return v1.last_butt_time - v2.last_butt_time
      }).reverse()
			data.msguser = msguser
			
			// 获取用户列表
			var allUser = await this.getAlluser(csuser.cs_id)
			if (allUser.status !== 1) allUser.msg = []
			data.statis = this.calcUsers(allUser.msg)
			data.alluser = allUser.msg
			
			// 连接socket
			console.log($ws)
			$ws.emit('re_connect', { // 链接socket.io
        user: data.csuser,
        type: config.type
      })
			return Promise.resolve({
        status: 1,
        msg: '资源加载完成',
				data: data
      })
		} catch (e) {
			console.log(e)
			return Promise.resolve({
				status: -1,
				msg: '加载失败'
			})
		}
	}
}
// export default {}